<template>
    <el-dialog :close-on-click-modal="false"  ref="dialogform" :title="title" :visible.sync="dialogVisible" width="40%"
        :before-close="handleClose">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
            <el-form-item label="仓库" prop="stordocNo">
                <el-select v-model="ruleForm.stordocNo" class="w100p" clearable filterable placeholder="请选择仓库">
                    <el-option v-for="item, index in stordoc" :key="index" :label="item.stordocName"
                        :value="item.stordocNo"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="仓库管员" prop="cwhsmanagerName">
                <el-select v-model="ruleForm.cwhsmanagerName" class="w100p" clearable filterable placeholder="请选择仓库管员">
                    <el-option v-for="item, index in person" :key="index" :label="item.person.name"
                        :value="item.person.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="盘点人" prop="countoperatorName">
                <el-select v-model="ruleForm.countoperatorName" class="w100p" clearable filterable placeholder="请选择盘点人">
                    <el-option v-for="item, index in person" :key="index" :label="item.person.name"
                        :value="item.person.name"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="盘点日期" prop="dcountDate">
                <el-date-picker v-model="ruleForm.dcountDate" align="right" type="date" placeholder="选择日期"
                    :picker-options="pickerOptions">
                </el-date-picker>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="cancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
        </span>
    </el-dialog>
</template>
<script>
import warehouseManage from '@/api/digtalOperation/warehouseManage';
export default {
    components: {},
    props: {
        addEditFlag: {
            type: String,
            default: () => {
                return 'add'
            }
        },
        data: {
            type: Object,
            default: () => {
                return {}
            }
        },
        title: {
            type: String,
            default: () => {
                return '操作表单'
            }
        },
        dialogVisible: {
            type: Boolean,
            default: () => {
                return false
            }
        }
    },
    data() {
        return {
            pickerOptions: {
                disabledDate(time) {
                    return time.getTime() > Date.now();
                },
                shortcuts: [{
                    text: '今天',
                    onClick(picker) {
                        picker.$emit('pick', new Date());
                    }
                }, {
                    text: '昨天',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24);
                        picker.$emit('pick', date);
                    }
                }, {
                    text: '一周前',
                    onClick(picker) {
                        const date = new Date();
                        date.setTime(date.getTime() - 3600 * 1000 * 24 * 7);
                        picker.$emit('pick', date);
                    }
                }]
            },
            addEditApi: warehouseManage.add,
            ruleForm: {},
            rules: {
                stordocNo: [
                    { required: true, message: '请选择仓库', trigger: 'change' }
                ],
                cwhsmanagerName: [
                    { required: true, message: '请选择仓库管员', trigger: 'change' }
                ],
                countoperatorName: [
                    { required: true, message: '请选择盘点人', trigger: 'change' }
                ],
                dcountDate: [
                    { required: true, message: '请选择盘点日期', trigger: 'change' }
                ]
            }
        }
    },
    computed: {
        person() {
            return this.$store.state.initData.person || [];
        },
        stordoc() {
            return this.$store.state.initData.stordoc || [];
        },
    },
    watch: {
        dialogVisible(v) {
            v && this.initData()
        }
    },
    mounted() {
    },
    methods: {
        initData() {
            this.resetForm()
            this.addEditApi = warehouseManage.add
        },
        //提交表单
        submitForm() {
            this.$refs.ruleForm.validate((valid) => {
                if (valid) {
                    this.addEditApi({ ...this.ruleForm, dcountDate: new Date(this.ruleForm.dcountDate).getTime() }).then(res => {
                        if (res.code == 1) {
                            this.$message.success('操作成功')
                        } else {
                            this.$message.warning('操作失败')
                        }
                        this.cancel()
                    }).catch(err => {
                        this.$message.warning('操作失败')
                    })
                } else {
                    return false;
                }
            })
        },
        //重置
        resetForm() {
            this.ruleForm = {};
            this.$refs.ruleForm && this.$refs.ruleForm.resetFields()
        },
        //取消关闭
        cancel() {
            this.resetForm()
            this.$emit("closeDialog")
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => { this.resetForm(); this.cancel() })
                .catch(_ => { });
        }
    }
};
</script>
<style lang="scss" scoped>
.tag {
    margin: 5px 0;
    margin-right: 5px;
    cursor: pointer;
}
</style>
  